/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JoinDBPane.java
 *
 * Created on Aug 29, 2009, 10:14:42 PM
 */
package org.dbdir.client.gui;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.dbdir.Client;

/**
 *
 * @author thotheolh
 */
public class JoinDBPane extends javax.swing.JPanel {

    Client client;
    MainFrame frame;

    /** Creates new form JoinDBPane */
    public JoinDBPane(Client client, MainFrame frame) {
        initComponents();
        this.client = client;
        this.frame = frame;
        this.setSize(326, 129);
    }

    public void init() {
        dbCB.removeAllItems();
        ArrayList dbList = client.getAvDBList();
        if ((dbList != null) && (!dbList.isEmpty())) {
            for (int i = 0; i < dbList.size(); i++) {
                dbCB.addItem(dbList.get(i).toString());
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        dbCB = new javax.swing.JComboBox();
        bottomPane = new javax.swing.JPanel();
        leftPane = new javax.swing.JPanel();
        mainBtn = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        rightPane = new javax.swing.JPanel();
        joinBtn = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jLabel1.setText("DB Profile: ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dbCB, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(dbCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        bottomPane.setLayout(new java.awt.BorderLayout());

        leftPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        mainBtn.setText("Back to Main");
        mainBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mainBtnActionPerformed(evt);
            }
        });
        leftPane.add(mainBtn);

        jButton1.setText("DB");
        jButton1.setToolTipText("Manage DB page");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        leftPane.add(jButton1);

        bottomPane.add(leftPane, java.awt.BorderLayout.WEST);

        rightPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        joinBtn.setText("Join DB");
        joinBtn.setToolTipText("Join existing DB Profile");
        joinBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joinBtnActionPerformed(evt);
            }
        });
        rightPane.add(joinBtn);

        bottomPane.add(rightPane, java.awt.BorderLayout.EAST);

        add(bottomPane, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void mainBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mainBtnActionPerformed
        // TODO add your handling code here:
        frame.getActionPane();
}//GEN-LAST:event_mainBtnActionPerformed

    private void joinBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_joinBtnActionPerformed
        // TODO add your handling code here:

        if (dbCB.getItemCount() != 0) {
            String selectedProf = dbCB.getSelectedItem().toString();
            if (client.joindb(selectedProf)) {
                JOptionPane.showMessageDialog(this, "You have successfully join db profile: " + selectedProf, "Join DB Successful", JOptionPane.INFORMATION_MESSAGE);
                init();
            } else {
                JOptionPane.showMessageDialog(this, "You have failed to join db profile: " + selectedProf, "Join DB Failed", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_joinBtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        frame.getManDBPane();
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPane;
    private javax.swing.JComboBox dbCB;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton joinBtn;
    private javax.swing.JPanel leftPane;
    private javax.swing.JButton mainBtn;
    private javax.swing.JPanel rightPane;
    // End of variables declaration//GEN-END:variables
}
